﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Org.BouncyCastle.Crypto;
using System.Net;
using WomanMotion.WebApi.Api.Utility;
using WomanMotion.WebApi.Infrastructure.DataBaseCommon;
using WomanMotion.WebApi.Infrastructure.UnifyResult;
using WomanMotion.WebApi.Model.Dtos;
using WomanMotion.WebApi.Model.Dtos.Comment;
using WomanMotion.WebApi.Model.Dtos.Contents;
using WomanMotion.WebApi.Model.Entitys;
using WomanMotion.WebApi.Service;
using WomanMotion.WebApi.Service.IService;
using static SKIT.FlurlHttpClient.Wechat.Api.Models.CgibinUserInfoBatchGetRequest.Types;
using static SKIT.FlurlHttpClient.Wechat.Api.Models.ShopFundsGetBalanceResponse.Types;

namespace WomanMotion.WebApi.Api.Controllers
{
    /// <summary>
    /// 评论管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [XSSFilter]
    [ApiController]
    [DescriptionName("评论管理", true, 90)]
    public class CommentController : BaseController
    {
        private readonly ICommentService _comment;
        private readonly IHttpContextAccessor _httpContextAccessor;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="comment"></param>
        public CommentController(ILogger<CommentController> logger, ICommentService comment, IHttpContextAccessor httpContextAccessor) : base(logger)
        {
            _comment = comment;
            _httpContextAccessor = httpContextAccessor;
        }

        /// <summary>
        /// 创建评论
        /// </summary>
        /// <param name="dto">DTO</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate<CommentVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("创建评论", true, "评论管理", "List", "CommentList")]
        public async Task<IResultOutput> Created(CreatedCommentDTO dto)
        {
            var res = await _comment.CreatedAsync(dto);
            if (res != null)
            {
                return ResultOutput.Ok(res, "操作成功");
            }
            else
            {
                return ResultOutput.NotOk("操作失败");
            }
        }

        /// <summary>
        /// 启用评论
        /// </summary>
        /// <param name="ids">主键ID集合</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate), (int)HttpStatusCode.OK)]
        [DescriptionName("启用评论")]
        public async Task<IResultOutput> Enable(PrimaryKeyDTO ids)
        {
            var res = await _comment.UpdateDisableAsync(DisableEnum.Enable, ids.Ids);
            if (res > 0)
            {
                return ResultOutput.Ok(new object(), "操作成功");
            }
            else
            {
                return ResultOutput.NotOk("操作失败");
            }
        }

        /// <summary>
        /// 禁用评论
        /// </summary>
        /// <param name="ids">主键ID集合</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate), (int)HttpStatusCode.OK)]
        [DescriptionName("禁用评论")]
        public async Task<IResultOutput> Disable(PrimaryKeyDTO ids)
        {
            var res = await _comment.UpdateDisableAsync(DisableEnum.Disable, ids.Ids);
            if (res > 0)
            {
                return ResultOutput.Ok(new object(), "操作成功");
            }
            else
            {
                return ResultOutput.NotOk("操作失败");
            }
        }

        /// <summary>
        /// 前端用户禁用评论
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(SwaggerResultTemplate), (int)HttpStatusCode.OK)]
        [DescriptionName("前端用户禁用评论")]
        public async Task<IResultOutput> LeadingDisable(long id)
        {
            var userid = _httpContextAccessor.HttpContext.User.Claims.Where(m => m.Type == "UserId").First().Value;
            var entity = await _comment.GetEntityAsync(m => m.Id == id);
            if (entity == null)
            {
                return ResultOutput.NotOk("数据不存在");
            }
            if (entity.Createdby != long.Parse(userid))
            {
                return ResultOutput.NotOk("不能删除别人的评论");
            }
            //当为顶级评论
            if (entity.Parentid == 0 || entity.Parentid == null)
            {
                List<long> ids = new List<long>();

                //查询下级评论
                var children = await _comment.GetListAsync(m => m.Parentid == id);
                if (children.Count > 0)
                {
                    ids = children.Select(m => m.Id).ToList();
                }
                ids.Add(id);
                var res = await _comment.UpdateDisableAsync(DisableEnum.Disable, ids.ToArray());
                if (res > 0)
                {
                    return ResultOutput.Ok(new object(), "操作成功");
                }
                else
                {
                    return ResultOutput.NotOk("操作失败");
                }
            }
            else//不为顶级评论
            {
                long[] ids = { id };
                var res = await _comment.UpdateDisableAsync(DisableEnum.Disable, ids);
                if (res > 0)
                {
                    return ResultOutput.Ok(new object(), "操作成功");
                }
                else
                {
                    return ResultOutput.NotOk("操作失败");
                }
            }
        }

        /// <summary>
        /// 更新评论
        /// </summary>
        /// <param name="dto">dto</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate<CommentVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("更新评论")]
        public async Task<IResultOutput> Update(UpdateCommentDTO dto)
        {
            var res = await _comment.UpdateAsync(dto);
            if (res == null)
            {
                return ResultOutput.NotOk("操作失败");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 根据ID获取评论信息
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(SwaggerResultTemplate<CommentVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("根据ID获取评论信息")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetById(long id)
        {
            var res = await _comment.GetByIdAsync(id);
            if (res == null)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 根据ID获取多表连接评论信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(SwaggerResultTemplate<CommentVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("根据ID获取多表连接评论信息")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetCommonById(long id)
        {
            var res = await _comment.GetByIDNavAsync(id);
            if (res == null)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 分页获取评论数据
        /// </summary>
        /// <param name="pars">查询条件</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate<PageViewBase<List<CommentVM>>>), (int)HttpStatusCode.OK)]
        [DescriptionName("分页获取评论数据")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetPageList(QueryParameterBase pars)
        {
            var res = await _comment.GetPageNavListAsync(pars);
            if (res.Data == null || res.Data.Count == 0)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        ///根据当前评论ID查询出所属下一级的评论
        /// </summary>
        /// <param name="parentid">评论id</param>
        /// <returns>下一级评论</returns>
        [HttpGet]
        [ProducesResponseType(typeof(SwaggerResultTemplate<List<CommentPageVM>>), (int)HttpStatusCode.OK)]
        [DescriptionName("根据当前评论ID查询出所属下一级的评论")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetNextList(long parentid)
        {
            var res = await _comment.GetByParentIdAsync(parentid);
            if (res.Count == 0)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 返回前端文章的所有评论数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(SwaggerResultTemplate<CommentTreeListVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("返回前端文章的所有评论数据")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetTreeList(long id)
        {
            var res = await _comment.GetTreeByIdAsync(id);
            if (res.commentList == null || res.commentList.Count == 0)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }
    }
}